package com.neu.af.controller;

import com.github.pagehelper.PageInfo;
import com.neu.af.common.base.BaseController;
import com.neu.af.common.base.BaseModel;
import com.neu.af.common.base.BaseModelJson;
import com.neu.af.common.exception.BusinessException;
import com.neu.af.common.validationGroup.DeleteGroup;
import com.neu.af.common.validationGroup.InsertGroup;
import com.neu.af.common.validationGroup.UpdateGroup;
import com.neu.af.dataFilter.entity.*;
import com.neu.af.dataFilter.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/dataOp")
public class DataOpController extends BaseController {
    @Autowired
    DataService dataService;
    @Autowired
    TagRuleAlarmService tagRuleAlarmService;
    @Autowired
    AbDataService abDataService;
    @Autowired
    RuleService ruleService;
    @Autowired
    MonitorStatusService monitorStatusService;
    @Autowired
    TagService tagService;

    @RequestMapping("/updateTagRuleAlarm")
    public BaseModel updateTagRuleAlarm() {
        tagRuleAlarmService.generateTagRuleAlarm();
        BaseModel result = new BaseModel();
        result.code = 200;
        result.message = "success";
        return result;
    }

    @RequestMapping("/getAllRule")
    public BaseModelJson<Map> getAllRule() {
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = new HashMap<>();
        List<Rule> rules = ruleService.getAll();
        map.put("rules", rules);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }

    @RequestMapping("/getAllRuleByPage")
    public BaseModelJson<PageInfo<Rule>> getAllRuleByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<Rule>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        Map<String, Object> map = new HashMap<>();
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageInfo<Rule> rules = ruleService.getAllByFilter(pageNum, pageSize, map);
        result.code = 200;
        result.message = "success";
        result.data = rules;
        return result;
    }

    @RequestMapping("/updateRule")
    public BaseModel updateRule(@Validated({UpdateGroup.class}) @RequestBody Rule rule, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw BusinessException.RULE_CONRAINS_OR_CLASS_NULL.newInstance(this.getErrorResponse(bindingResult),
                    new Object[]{rule.toString()});
        }
        if (rule.getAlarm_class() != 1 && rule.getAlarm_class() != 2) {
            throw BusinessException.ALARM_CLASS_ILLEGAL;
        }
        if (ruleService.getById(rule.getId()).getContains().equals(rule.getContains())
                && ruleService.getById(rule.getId()).getAlarm_class()==(rule.getAlarm_class())){
            throw BusinessException.UPDATE_NOT_CHANGE;
        }
        else {

            int i = ruleService.update(rule);
            if (i == 1) {
                BaseModel result = new BaseModel();
                result.code = 200;
                result.message = "success";
                return result;
            } else {
                throw BusinessException.UPDATE_FAIL;
            }
        }

    }

    @RequestMapping("/insertRule")
    public BaseModel insertRule(@Validated({InsertGroup.class}) @RequestBody Rule rule, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw BusinessException.RULE_CONRAINS_OR_CLASS_NULL.newInstance(this.getErrorResponse(bindingResult),
                    new Object[]{rule.toString()});
        } else {
            if(ruleService.getByContains(rule.getContains()) != null) {
                throw BusinessException.RULE_DUPILICATED;
            }
            if(rule.getAlarm_class() != 1 && rule.getAlarm_class() != 2) {
                throw BusinessException.ALARM_CLASS_ILLEGAL;
            }
            int i = ruleService.insert(rule);
            if (i == 1) {
                BaseModel result = new BaseModel();
                result.code = 200;
                result.message = "success";
                return result;
            } else {
                throw BusinessException.INSERT_FAIL;
            }
        }
    }

    @RequestMapping("/deleteRule")
    public BaseModel deleteRule(@Validated({DeleteGroup.class}) @RequestBody Rule rule, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw BusinessException.RULE_CONRAINS_OR_CLASS_NULL.newInstance(this.getErrorResponse(bindingResult),
                    new Object[]{rule.toString()});
        } else {
            int i = ruleService.delete(rule.getId());
            if (i == 1) {
                BaseModel result = new BaseModel();
                result.code = 200;
                result.message = "success";
                return result;
            } else {
                throw BusinessException.DELETE_RULE_FAIL;
            }
        }
    }

    @RequestMapping("/searchRule")
    public BaseModelJson<Map> searchRule(@RequestBody Rule rule) {
        List<Rule> rules = ruleService.getByContainsLike(rule.getContains());
        if(rules.size() == 0) {
            throw BusinessException.SEARCH_RULE_NULL;
        }
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = new HashMap<>();
        map.put("rules", rules);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }

    @RequestMapping("searchRuleByPage")
    public BaseModelJson<PageInfo<Rule>> searchRuleByPage(@RequestBody Map<String,Object> allMap) {
        BaseModelJson<PageInfo<Rule>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Map<String, Object> filterMap = (Map<String, Object>) allMap.get("filterMap");
        PageInfo<Rule> rules = ruleService.getAllByFilter(pageNum, pageSize, filterMap);
        if(rules.getSize() == 0) {
            throw BusinessException.SEARCH_RULE_NULL;
        }
        result.code = 200;
        result.message = "success";
        result.data = rules;
        return result;
    }


    @RequestMapping("/getAllAbnormalMonitor")
    public BaseModelJson<Map> getAllAbnormalMonitor() {
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = new HashMap<>();
        List<MonitorStatus> monitorStatus = monitorStatusService.getAllAbnormal();
        map.put("monitorStatus", monitorStatus);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }

    @RequestMapping("/getAllAbnormalMonitorByPage")
    public BaseModelJson<PageInfo<MonitorStatus>> getAllAbnormalMonitorByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<MonitorStatus>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageInfo<MonitorStatus> monitorStatusPageInfo = monitorStatusService.getAllAbnormal(pageNum, pageSize);
        result.code = 200;
        result.message = "success";
        result.data = monitorStatusPageInfo;
        return result;
    }

    @RequestMapping("/getAllMonitor")
    public BaseModelJson<Map> getAllMonitor() {
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = new HashMap<>();
        List<MonitorStatus> monitorStatus = monitorStatusService.getAll();
        map.put("monitorStatus", monitorStatus);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }

    @RequestMapping("/getAbnormalMonitorByFilter")
    public BaseModelJson<Map> getAbnormalMonitorByFilter(@RequestBody Map<String, Object> map) {
        BaseModelJson<Map> result = new BaseModelJson<>();
        List<MonitorStatus> m = monitorStatusService.getAbnormalMonitorByFilter(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("monitorStatus", m);
        result.code = 200;
        result.message = "success";
        result.data = map2;
        return result;
    }

    @RequestMapping("/getAbnormalMonitorByFilterByPage")
    public BaseModelJson<PageInfo<MonitorStatus>> getAbnormalMonitorByFilterByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<MonitorStatus>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Map<String, Object> filterMap = (Map<String, Object>) allMap.get("filterMap");
        PageInfo<MonitorStatus> monitorStatusPageInfo = monitorStatusService.getAllByFilter(pageNum, pageSize, filterMap);
        if(monitorStatusPageInfo.getSize() == 0) {
            throw BusinessException.SEARCH_ABNORMAL_NULL;
        }
        result.code = 200;
        result.message = "success";
        result.data = monitorStatusPageInfo;
        return result;
    }

    @RequestMapping("/getInterupted")
    public BaseModelJson<Map> getInterupted() {
        BaseModelJson<Map> result = new BaseModelJson<>();
        List<MonitorStatus> list = monitorStatusService.getDataSourceInterupt();
        Map<String, Object> map2 = new HashMap<>();
        if(list.size()==0 || list==null) {
            map2.put("info", "暂无数据源中断");
        }
        else {
            map2.put("interuptedSources",list);
        }
        result.code = 200;
        result.message = "success";
        result.data = map2;
        return result;
    }

    @RequestMapping("/getInteruptedByPage")
    public BaseModelJson<PageInfo<MonitorStatus>> getInteruptedByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<MonitorStatus>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Map<String, Object> filterMap = (Map<String, Object>) allMap.get("filterMap");
        PageInfo<MonitorStatus> monitorStatusPageInfo = monitorStatusService.getAllByFilter2(pageNum, pageSize, filterMap);
        if(monitorStatusPageInfo.getSize() == 0) {
            throw BusinessException.SEARCH_ABNORMAL_NULL;
        }
        result.code = 200;
        result.message = "success";
        result.data = monitorStatusPageInfo;
        return result;
    }

    @RequestMapping("/judgeInterupt")
    public BaseModel judgeInterupt(){
        BaseModel result = new BaseModel();
        result.code = 200;
        if(monitorStatusService.searchStatus5()>6000){
            result.message = "数据源中断";
        }
        else result.message="数据源连接正常";
        return result;
    }

    @RequestMapping("/deleteTag")
    public BaseModel deleteTag( @RequestBody Tag tag) {
        int i = tagService.delete(tag);
        if (i == 1) {
            BaseModel result = new BaseModel();
            result.code = 200;
            result.message = "success";
            return result;
        } else {
            throw BusinessException.DELETE_TAG_FAIL;
        }
    }

    @RequestMapping("/insertTag")
    public BaseModel insertTag(@RequestBody Tag tag) {
        int i = tagService.insert(tag);
        if (i == 1) {
            BaseModel result = new BaseModel();
            result.code = 200;
            result.message = "success";
            return result;
        } else {
            throw BusinessException.SEARCH_DESCRIPTOR_FAIL;
        }
    }

    @RequestMapping("/getAllTagByPage")
    public BaseModelJson<PageInfo<Tag>> getAllTagByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<Tag>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");
        Map<String, Object> map = new HashMap<>();
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageInfo<Tag> tags = tagService.getAllByFilter(pageNum, pageSize, map);
        result.code = 200;
        result.message = "success";
        result.data = tags;
        return result;
    }

    @RequestMapping("/searchTagByPage")
    public BaseModelJson<PageInfo<Tag>> searchTagByPage(@RequestBody Map<String, Object> allMap) {
        BaseModelJson<PageInfo<Tag>> result = new BaseModelJson<>();
        Integer pageNum = (Integer) allMap.get("pageNum");
        Integer pageSize = (Integer) allMap.get("pageSize");

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Map<String, Object> map = (Map<String, Object>)allMap.get("filterMap");
        PageInfo<Tag> tags = tagService.getAllByFilter(pageNum, pageSize, map);
        result.code = 200;
        result.message = "success";
        result.data = tags;
        return result;
    }

    @RequestMapping("/getAlarmClassByType")
    public BaseModelJson<Map> getAlarmClassByType(@RequestBody Map<String, Object> serachMap) {
        BaseModelJson<Map> result = new BaseModelJson<>();

        List<AbData> list = abDataService.getAlarmClassByType(serachMap);
        Map<String, Object> map2 = new HashMap<>();
        if(list.size()==0 || list==null) {
            map2.put("info", "暂无新数据");
        }
        else {
            map2.put("abdata",list);
        }
        result.code = 200;
        result.message = "success";
        result.data = map2;
        return result;
    }

    @RequestMapping("/getClassOneAlarm")
    public BaseModelJson<Map> getClassOneAlarm(@RequestBody Map<String, Object> serachMap) {
        BaseModelJson<Map> result = new BaseModelJson<>();

        Map<String,List<AbData>> list = abDataService.getClassOneAlarm(serachMap);
        Map<String, Object> map2 = new HashMap<>();
        if(list.size()==0 || list==null) {
            map2.put("info", "暂无新数据");
        }
        else {
            map2.put("ClassOneAlarm",list);
        }
        result.code = 200;
        result.message = "success";
        result.data = map2;
        return result;
    }

    @RequestMapping("/getAlarmClassByGroup")
    public BaseModelJson<Map> getAlarmClassByGroup(@RequestBody Map<String, Object> serachMap) {
        BaseModelJson<Map> result = new BaseModelJson<>();

        List<AbData> list = abDataService.getAlarmClassByGroup(serachMap);
        Map<String, Object> map2 = new HashMap<>();

        map2.put("abdata",list);

        result.code = 200;
        result.message = "success";
        result.data = map2;
        return result;
    }

    @RequestMapping("/updateKoujing")
    public BaseModelJson<Map> updateKoujing(@RequestBody Map<String, Object> serachMap){

        abDataService.updateKoujing(serachMap);
        BaseModelJson<Map> result = new BaseModelJson<>();
        result.code = 200;
        result.message = "success";
        return result;
    }

    // 得到当前的统计口径
    @RequestMapping("/getKoujing")
    public int getKoujing(){
        System.out.println(abDataService.getKoujing());
        return abDataService.getKoujing();
    }
}






